package pl.pkolodziej.platform.controller.course;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import pl.pkolodziej.platform.service.course.CourseService;
import pl.pkolodziej.platform.service.course.UserCourseService;
import pl.pkolodziej.platform.service.user.UserService;
import pl.pkolodziej.platform.vo.course.Course;
import pl.pkolodziej.platform.vo.course.SelectedCourse;
import pl.pkolodziej.platform.vo.course.UserCourseInfo;
import pl.pkolodziej.platform.vo.user.User;
import pl.pkolodziej.platform.vo.user.UserInfo;
import pl.pkolodziej.utils.MyTime;

@Controller
@RequestMapping(value="/course")
public class CourseController {

	@Autowired
	CourseService courseService;
	
	@Autowired
	UserService userService;
	
	@Autowired
	UserCourseService userCourseService;
	
	private final Logger logger = Logger.getLogger(getClass());
	
	@RequestMapping(value="/results")
	public String showCourseSummary(Map<String, Object> map){
		List<UserCourseInfo> uciList = new ArrayList<UserCourseInfo>();
		User u = new User();
		u.setUsername(SecurityContextHolder.getContext().getAuthentication().getName());
		UserInfo ui = userService.getUserInfo(u);

		for (Object object : ui.getUserCourseInfo().toArray()) {
			UserCourseInfo uci = (UserCourseInfo) object;
			uci.setPagesProgress((uci.getPages()*100)/uci.getCourse().getPages());
			uci.setStartTestProgress((uci.getStarttest()*100)/uci.getCourse().getStartTestMaxScore());
			uci.setEndTestProgress((uci.getEndtest()*100)/uci.getCourse().getEndTestMaxScore());
			uciList.add(uci);
		}
		
		map.put("userCourseInfoList",uciList);
		return "course/summary";
	}
	
	@RequestMapping(value="/listAll")
	public String showAllCourses(Map<String, Object> map){
		int i=1;
		List<Course> listCourse = new ArrayList<Course>();
		for(Course c : courseService.listCourses()){
			if(i>8)
				break;
			listCourse.add(c);
			i++;
		}
		
		map.put("coursesList",listCourse.toArray());
		return "course/listAll";
	}
	
	@RequestMapping(value="/list")
	public String showMyCoursesList(Map<String, Object> map){
		List<UserCourseInfo> uciList = new ArrayList<UserCourseInfo>();
		User u = new User();
		u.setUsername(SecurityContextHolder.getContext().getAuthentication().getName());
		UserInfo ui = userService.getUserInfo(u);

		for (Object object : ui.getUserCourseInfo().toArray()) {
			UserCourseInfo uci = (UserCourseInfo) object;
			uci.setPagesProgress((uci.getPages()*100)/uci.getCourse().getPages());
			uci.setStartTestProgress((uci.getStarttest()*100)/uci.getCourse().getStartTestMaxScore());
			uci.setEndTestProgress((uci.getEndtest()*100)/uci.getCourse().getEndTestMaxScore());
			uciList.add(uci);
		}
		map.put("userInfo", ui);
		map.put("userCourseInfoList",uciList);
		return "course/list";
	}
	
	@RequestMapping(value="/schedule")
	public String showSechedule(Map<String, Object> map){
		List<String> msgList = new ArrayList<String>();
		User u = new User();
		u.setUsername(SecurityContextHolder.getContext().getAuthentication().getName());
		UserInfo ui = userService.getUserInfo(u);

		if(ui.getMsg1().length()>0)
			msgList.add(ui.getMsg1());
		if(ui.getMsg2().length()>0)
			msgList.add(ui.getMsg2());
		if(ui.getMsg3().length()>0)
			msgList.add(ui.getMsg3());
		if(ui.getMsg4().length()>0)
			msgList.add(ui.getMsg4());
		if(ui.getMsg5().length()>0)
			msgList.add(ui.getMsg5());
		if(ui.getMsg6().length()>0)
			msgList.add(ui.getMsg6());
		if(ui.getMsg7().length()>0)
			msgList.add(ui.getMsg7());
		if(ui.getMsg8().length()>0)
			msgList.add(ui.getMsg8());
		if(ui.getMsg9().length()>0)
			msgList.add(ui.getMsg9());
		if(ui.getMsg10().length()>0)
			msgList.add(ui.getMsg10());
		
		map.put("msgList",msgList);
		return "course/schedule";
	}
	
	@RequestMapping(value="/show/{id}")
	public String showCourse(@PathVariable("id") String id){
		int lastPage = 1;
		String username = (SecurityContextHolder.getContext().getAuthentication().getName());
		Timestamp lastattempt = MyTime.getCurrentTimestamp();
		userCourseService.updateUserCourseLastattempt(username, Integer.parseInt(id), lastattempt);
		
		User u = new User();
		u.setUsername(username);
		UserInfo ui = userService.getUserInfo(u);
		
		for (Object object : ui.getUserCourseInfo().toArray()) {
			UserCourseInfo uci = (UserCourseInfo) object;
			if(uci.getCourse().getCourse_id()==Integer.parseInt(id)){
				lastPage = uci.getPages();
				break;
			}
		}
		
		Map<Integer, UserCourseInfo> uciMap = new HashMap<Integer, UserCourseInfo>();
		for (Object object : ui.getUserCourseInfo().toArray()) {
			UserCourseInfo uci = (UserCourseInfo) object;
			uciMap.put(uci.getCourse().getCourse_id(), uci);
		}
		
		SelectedCourse.course_id = Integer.parseInt(id);
		SelectedCourse.username = username;
		SelectedCourse.lastPage = lastPage;


		logger.info("CONVERSA START COURSE " + username + " : " + SelectedCourse.course_id);
		
		if(SelectedCourse.course_id<100 || (SelectedCourse.course_id>100 && uciMap.get((SelectedCourse.course_id-100))!=null && !uciMap.get((SelectedCourse.course_id-100)).getStarttest_done()))
			return "course/show";
		else{
			logger.info("CONVERSA TEST ALREADY DONE!!!" + username + " : " + SelectedCourse.course_id);
			return "course/testDone";
		}
	}
	

	
	@RequestMapping(value="/info/{id}")
	public String showInfo(@PathVariable("id") String id, Map<String, Object> map){
		map.put("course",courseService.listCourses().get(Integer.parseInt(id)-1));
		return "course/info";
	}
	
	@RequestMapping(value="/meeting/{id}")
	public String showMeeting(@PathVariable("id") String id, Map<String, Object> map){
		map.put("course",courseService.listCourses().get(Integer.parseInt(id)-1));
		return "course/meeting";
	}
}
